package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.ProjectListAdapter;
import com.longcai.piaojia.conn.Conn;
import com.longcai.piaojia.conn.ProjectListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/7.
 */
public class ProjectActivity extends BaseActivity {


    @BoundView(R.id.project_lv)
    private ListView project_lv;

    private ProjectListAdapter adapter;

    private List<ProjectListGet.Project> lists = new ArrayList<>();

    private ProjectListGet projectListGet = new ProjectListGet(new AsyCallBack<ProjectListGet.ProjectEntity>() {

        @Override
        public void onSuccess(String toast, int type, ProjectListGet.ProjectEntity projectEntity) throws Exception {

            lists.addAll(projectEntity.projectList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_project);

        setBackTrue();

        setTitleName(getString(R.string.project_title_text));


        project_lv.setDivider(ContextCompat.getDrawable(context, R.color.background_bg_color));

        project_lv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(20));

        project_lv.setAdapter(adapter = new ProjectListAdapter(context, lists));

        project_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*
             if (lists.get(position).id.equals("11")) {

                    startActivity(new Intent(ProjectActivity.this, LatestActivity.class));

                } else {

                    Bundle bundle = new Bundle();

                    bundle.putString("title", lists.get(position).title);
                    bundle.putString("url", Conn.SERVICE + Conn.ACTIVE_WEB + "?id=" + lists.get(position).id);

//                Log.e("====url====", Conn.SERVICE + Conn.ACTIVE_WEB + "?id=" + lists.get(position).id);

                    startActivity(new Intent(ProjectActivity.this, ProjectDetailWebActivity.class).putExtras(bundle));

                }*/
                String userid = BaseApplication.BasePreferences.readUserId();

                if (!BaseApplication.BasePreferences.readIsLogin()) {
                    startActivity(new Intent(ProjectActivity.this, LoginActivity.class));
                }else


                if (lists.get(position).type.equals("1")) {

                    Intent intent = new Intent();
                    intent.putExtra("url", lists.get(position).weburl + "?id=" + BaseApplication.BasePreferences.readUserId());
                    intent.putExtra("title", lists.get(position).title);

                    intent.setClass(ProjectActivity.this, ChainActivity.class);
                    startActivity(intent);

                   /* String userid = BaseApplication.BasePreferences.readUserId();
                    //  String token = UtilMD5.MD5Encode(UtilMD5.MD5Encode(userid, "UTF-8") + UtilMD5.MD5Encode(Integer.parseInt(userid) % 7 + "", "UTF-8"), "UTF-8");
                    String token = UtilMD5.MD5Encode(UtilMD5.MD5Encode(userid, "UTF-8") + UtilMD5.MD5Encode(Integer.parseInt(userid) % 7 + "", "UTF-8"), "UTF-8");

                    Bundle bundle1 = new Bundle();

                    bundle1.putString("url",windows.url + "?id=" + userid);
                    bundle1.putString("title", windows.title);

                    startActivity(new Intent(getActivity(), IndexChainActivity.class).putExtras(bundle1));*/
                } else if (lists.get(position).id.equals("11")) {

                    startActivity(new Intent(ProjectActivity.this, LatestActivity.class));

                } else {

                    Bundle bundle = new Bundle();

                    bundle.putString("title", lists.get(position).title);
                    bundle.putString("url", Conn.SERVICE + Conn.ACTIVE_WEB + "?id=" + lists.get(position).id);

//                Log.e("====url====", Conn.SERVICE + Conn.ACTIVE_WEB + "?id=" + lists.get(position).id);

                    startActivity(new Intent(ProjectActivity.this, ProjectDetailWebActivity.class).putExtras(bundle));

                }



              /*  if (lists.get(position).type.equals("1")) {


                    Intent intent = new Intent();
                    intent.putExtra("url", lists.get(position).weburl);
                    intent.setClass(LatestActivity.this, ChainActivity.class);
                    startActivity(intent);


                } else {

                    Bundle bundle = new Bundle();

                    bundle.putString("id", lists.get(position).id);
                    bundle.putString("title", lists.get(position).title);

                    startActivity(new Intent(LatestActivity.this, LatestDetailActivity.class).putExtras(bundle));
                }*/


            }
        });

        projectListGet.execute(this);
    }


}
